package cn.jy.nvr.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.NewProvider;
import cn.jy.nvr.domain.ProviderAssay;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.ProviderBodyCheck;
import cn.jy.nvr.domain.ProviderChestCheck;
import cn.jy.nvr.domain.ProviderCollection;
import cn.jy.nvr.domain.ProviderCost;
import cn.jy.nvr.domain.ProviderHeat;
import cn.jy.nvr.domain.ProviderRegister;
import cn.jy.nvr.domain.ProviderSampleAssay;
import cn.jy.nvr.mapper.NewProviderMapper;
import cn.jy.nvr.mapper.ProviderAssayMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.ProviderBodyCheckMapper;
import cn.jy.nvr.mapper.ProviderChestCheckMapper;
import cn.jy.nvr.mapper.ProviderCollectionMapper;
import cn.jy.nvr.mapper.ProviderCostMapper;
import cn.jy.nvr.mapper.ProviderHeatMapper;
import cn.jy.nvr.mapper.ProviderRegisterMapper;
import cn.jy.nvr.mapper.ProviderSampleAssayMapper;
import cn.jy.nvr.service.NewProviderQuartzJob;
import cn.jy.nvr.utils.TimeUtils;
@Service("NewProviderquartzJob")
public class NewProviderQuartzJobImpl implements NewProviderQuartzJob{
	@Autowired
	NewProviderMapper  providerMapper;
	@Autowired
	ProviderBaseInfoMapper baseInfoMapper;
	@Autowired
	ProviderRegisterMapper registerMapper;
	@Autowired 
	ProviderBodyCheckMapper bodyCheckMapper;
	@Autowired 
	ProviderSampleAssayMapper sampleAssayMapper;
	@Autowired
	ProviderChestCheckMapper chestCheckMapper;
	@Autowired
	ProviderAssayMapper assayMapper;
	@Autowired 
	ProviderCollectionMapper collectionMapper;
	@Autowired
	ProviderHeatMapper heatMapper;
	@Autowired
	ProviderCostMapper costMapper;
	
	Logger logger=Logger.getLogger(NewProviderQuartzJobImpl.class);
   public void work(){
	   System.out.println("----扫描今日新浆员信息——————");
	   String nowDate=new SimpleDateFormat("yyyy-MM-dd").format( new Date());
	   String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
	   System.out.println(nowDate);
	   //查询当日的新浆员人数
	   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
	   List<ProviderBaseInfo> providerBaseList = baseInfoMapper.getProviderBaseInfoByCreateRQ(nowDate);
	   for (ProviderBaseInfo providerBaseInfo : providerBaseList) {
		//查询NewProvider表中是否已经写入了这个人的信息了
		   logger.info(providerBaseInfo.getXM()+"----新浆员");
		   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		   NewProvider newProvider = providerMapper.getNewProviderByName(providerBaseInfo.getXM(), nowDate);
		   if(newProvider==null||newProvider.getName()==null){
			   NewProvider insertProvider=new NewProvider();
			   insertProvider.setName(providerBaseInfo.getXM());
			   insertProvider.setcreateDate(nowDate);
			   
			  //查找该新浆员的建档时间
			   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			   ProviderRegister providerRegister = registerMapper.getProviderRegisterByGJZID(providerBaseInfo.getID(), nowDate);
			    if( providerRegister!=null&&providerRegister.getREGTIME()!=null){
			    	logger.info(providerBaseInfo.getXM()+"----新浆员----建档时间"+providerRegister.getREGTIME());
			    	String registerTime = TimeUtils.getIntegerTime(providerRegister.getREGTIME());
			    	insertProvider.setCreateTime(registerTime);
			    	
			    	
			    	//查找新浆员的体检时间
			    	 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			    	ProviderBodyCheck providerBodyCheck = bodyCheckMapper.getProviderBodyCheckByGJZID(providerBaseInfo.getID(), nowDate);
			    	if(providerBodyCheck!=null&&providerBodyCheck.getTJJSSJ()!=null){
			    		logger.info(providerBaseInfo.getXM()+"----新浆员----体检时间"+providerBodyCheck.getTJJSSJ());
			    		String bodyCheckTime = TimeUtils.getIntegerTime(providerBodyCheck.getTJJSSJ());
			    		insertProvider.setBodyCheckTime(bodyCheckTime);
			    		
				    	  
			    		//查找新浆员的小样采集时间
			    		 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			    		ProviderSampleAssay providerSampleAssay = sampleAssayMapper.getProviderSampleAssayByGJZID(providerBaseInfo.getID(), nowDate);
			    		if(providerSampleAssay!=null&&providerSampleAssay.getJLJSSJ()!=null){
			    			logger.info(providerBaseInfo.getXM()+"----新浆员----小样采集时间"+providerSampleAssay.getJLJSSJ());
			    			String sampleAssayTime = TimeUtils.getIntegerTime(providerSampleAssay.getJLJSSJ());
			    			insertProvider.setSampleAssayTime(sampleAssayTime);
			    			//查找新浆员胸片时间
			    			 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			    			ProviderChestCheck providerChestCheck = chestCheckMapper.getProviderChestCheckByGJZID(providerBaseInfo.getID(), nowDate);
			    			if(providerChestCheck!=null&&providerChestCheck.getXTSJ()!=null){
			    				logger.info(providerBaseInfo.getXM()+"----新浆员----胸片时间"+providerChestCheck.getXTSJ());
			    				     String chestCheckTime = TimeUtils.getIntegerTime(providerChestCheck.getXTSJ());
			    				     insertProvider.setChestCheckTime(chestCheckTime);
			    			}
			    		}
			    	}
			    }
			    logger.info("保存新浆员信息");
			   providerMapper.save(insertProvider);
		   }else{
			  
			   //判断现在的查询出来的newProvider是否有建档时间，如果没有就写入
			   if(newProvider.getCreateTime()==null){
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   ProviderRegister providerRegister = registerMapper.getProviderRegisterByGJZID(providerBaseInfo.getID(), nowDate);
				   if(providerRegister!=null&&providerRegister.getREGTIME()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----建档时间"+providerRegister.getREGTIME());
					   String registerTime = TimeUtils.getIntegerTime(providerRegister.getREGTIME());
					   newProvider.setCreateTime(registerTime);
				   }
			   }
			
		       
			 //判断现在的查询出来的newProvider是否有体检时间，如果没有就写入
			   if(newProvider.getBodyCheckTime()==null){
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   ProviderBodyCheck providerBodyCheck = bodyCheckMapper.getProviderBodyCheckByGJZID(providerBaseInfo.getID(), nowDate);
				   if(providerBodyCheck!=null&&providerBodyCheck.getTJJSSJ()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----体检时间"+providerBodyCheck.getTJJSSJ());
					   String bodyCheckTime = TimeUtils.getIntegerTime(providerBodyCheck.getTJJSSJ());
					   newProvider.setBodyCheckTime(bodyCheckTime);
				   }
			   }
			   //判断现在的查询出来的newProvider是否有小样采集时间，如果没有就写入
			   if(newProvider.getSampleAssayTime()==null){
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   ProviderSampleAssay providerSampleAssay = sampleAssayMapper.getProviderSampleAssayByGJZID(providerBaseInfo.getID(), nowDate);
				   if(providerSampleAssay!=null&&providerSampleAssay.getJLJSSJ()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----小样采集时间"+providerSampleAssay.getJLJSSJ());
					   String sampleAssayTime = TimeUtils.getIntegerTime(providerSampleAssay.getJLJSSJ());
					   newProvider.setSampleAssayTime(sampleAssayTime);
				   }
			   }
			   
			   //判断现在的查询出来的newProvider是否有胸片时间，如果没有就写入
			   if(newProvider.getChestCheckTime()==null){
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   ProviderChestCheck providerChestCheck = chestCheckMapper.getProviderChestCheckByGJZID(providerBaseInfo.getID(), nowDate);
				   if (providerChestCheck!=null&&providerChestCheck.getXTSJ()!=null) {
					   logger.info(providerBaseInfo.getXM()+"----新浆员----胸片时间"+providerChestCheck.getXTSJ());
					   String chestCheckTime = TimeUtils.getIntegerTime(providerChestCheck.getXTSJ());
					  newProvider.setChestCheckTime(chestCheckTime);
				   }
			   }
			
			 //判断现在的查询出来的newProvider是否有化检时间，如果没有就写入
			   if(newProvider.getAssayTime()==null){
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   ProviderAssay providerAssay = assayMapper.getProviderAssayByGJZID(providerBaseInfo.getID(), nowDate);
				   if(providerAssay!=null&&providerAssay.getJLJSSJ()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----化检时间"+providerAssay.getJLJSSJ());
					   String assayTime = TimeUtils.getIntegerTime(providerAssay.getJLJSSJ());
					   newProvider.setAssayTime(assayTime);
				   }
			   }
		
			   //判断现在的查询出来的newProvider是否有采浆时间，如果没有就写入
			   if(newProvider.getCollectionTime()==null){
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   ProviderCollection providerCollection = collectionMapper.getProviderCollectionByGJZID(providerBaseInfo.getID(), nowDate);
				   if(providerCollection!=null&&providerCollection.getEND_TIME()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----采浆时间"+providerCollection.getEND_TIME());
					   String collectionTime = TimeUtils.getIntegerTime(providerCollection.getEND_TIME());
					   newProvider.setCollectionTime(collectionTime);
				   }
			   }
			
			   //判断现在的查询出来的newProvider是否有热和时间，如果没有就写入
			   if(newProvider.getHeatTime()==null){
				   
			        DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				   ProviderHeat providerHeat = heatMapper.getProviderHeatByGJZID(providerBaseInfo.getCARDID(), nowDate);
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   if(providerHeat!=null&&providerHeat.getInStorageTime()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----热和时间"+providerHeat.getInStorageTime());
					   String heatTime = TimeUtils.getIntegerTime(providerHeat.getInStorageTime());
					   newProvider.setHeatTime(heatTime);
				   }
				 
			   }
			   
			 //判断现在的查询出来的newProvider是否有费用发放时间，如果没有就写入
			   if(newProvider.getCostTime()==null){
				 
			       DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				   ProviderCost providerCost = costMapper.getProviderCostByGJZIDFromJingYing(providerBaseInfo.getCARDID(), nowDate);
				   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				   if(providerCost!=null&&providerCost.getJSSJ()!=null){
					   logger.info(providerBaseInfo.getXM()+"----新浆员----热和时间"+providerCost.getJSSJ());
					   String costTime = TimeUtils.getIntegerTime(nowTime);
					   newProvider.setCostTime(costTime);
				   }
			   }
			   providerMapper.update(newProvider);   
		   }
		    
	}
	   
   }
	
}
